#!/usr/bin/env bash

set_proxy() {
	if [ -n "${HTTP_PROXY}" ]; then
		echo "Set http_proxy as ${HTTP_PROXY}"
		export http_proxy=${HTTP_PROXY}
	fi
}

clean_proxy() {
	if [ -n "${HTTP_PROXY}" ]; then
		echo "Clean http_proxy"
		export http_proxy=
	fi
}

on_tail() {
	source ${CUR_DIR}/common/tail.sh
}

load_user_sh() {
	local userdir=${CUR_DIR}/user
	if [ -d "${userdir}" ]; then
		for userfile in $(ls ${userdir}/*.sh); do
			source ${userfile}
		done
	fi
}

server_clean() {
	if [ -d "${CUR_TEMP}" ]; then
		rm "${CUR_TEMP}" -Rf
	fi
}

server_prepare() {
	if [ ! -d "${CUR_TEMP}" ]; then
		mkdir "${CUR_TEMP}"
	fi

	server_verify_timezone
	case $? in
		0)
			server_fix_timezone ${TIMEZONE}
			;;
		1)
			echo "Timezone is ${TIMEZONE}!"
			;;
	esac

	# if defined ignore packages
	if [ -n "${APT_IGN_PKGS}" ]; then
		apt-mark hold ${APT_IGN_PKGS}
	fi

	server_verify_upgrade
	case $? in
		0)
			server_upgrade
			;;
		1)
			echo "Not need upgrade!"
			;;
	esac
}

server_verify_upgrade() {
	local logfile=${CUR_TEMP}/upgrade.log
	if [ ! -f ${logfile} ]; then
		return 0
	fi
	local date=$(head -n 1 ${logfile})
	if [ "${date}" = ${TODAY} ]; then
		return 1
	else
		return 0
	fi
}

# Basic setup
server_upgrade() {
	local logfile=${CUR_TEMP}/upgrade.log
	apt-get update
	apt-get upgrade -y
	# this is require!!
	setver_require_install
	echo ${TODAY} > ${logfile}
}

server_verify_timezone() {
	local logfile=${CUR_TEMP}/timezone.log
	if [ ! -f ${logfile} ]; then
		return 0
	fi
	if [ ! -n ${TIMEZONE} ]; then
		return 0
	fi
	local log_timezone=$(head -n 1 ${logfile})
	if [ "${log_timezone}" = ${TIMEZONE} ]; then
		return 1
	else
		return 0
	fi
}

# Fix timezone
# @param $1 timezone
server_fix_timezone() {
	local TIMEZONE_FILE=/usr/share/zoneinfo/$1
	local logfile=${CUR_TEMP}/timezone.log
	if [ -f ${TIMEZONE_FILE} ]; then
		echo "Fix timezone in $1"
		echo "$1" > /etc/timezone
		dpkg-reconfigure -f noninteractive tzdata
		cp /usr/share/zoneinfo/$1 /etc/localtime
		echo "$1" > ${logfile}
	fi
}

setver_require_install() {
	apt-get install -y ${SERVER_REQUIRE_PKGS}
}


# read file
# @param $1 filepath
# if you want to keep the space, tab, return, you should:
# IFS=""
# tengine_sh=$(read_file ./tengine/tengine.sh.tpl)
# echo ${tengine_sh}
read_file() {
	local file=$1
	IFS=""
	local data=$(cat ${file})
	echo ${data}
}

# download file and uncompress it
# @param $1 workdir
# @param $2 filename
# @param $3 download url
download_uncompress() {
	local work_dir=$1
	local name=$2
	local url=$3
	local file=${name}.tar.gz
	local file_path=${work_dir}/${file}
	local dir=${work_dir}/${name}

	if [ ! -d "${work_dir}" ]; then
		mkdir "${work_dir}"
	fi

	cd "${work_dir}"

	# we do not trust the dir
	if [ -d "${dir}" ]; then
		rm "${dir}" -Rf
	fi

	if [ ! -f "${file_path}" ]; then
		echo "Download ${url}"
		wget ${url} -c
	fi

	echo "Uncompress ${file_path}"

	local filename=$(ls ${name}.*)
	local extname=${filename#${name}.}

	case "${extname}" in
		tar.gz)
			tar zxf ${file_path}
        	;;
		zip)
        	;;
        *)
			echo "unknow extname"
			;;
	esac
}

set_dns() {
	local conf=/etc/resolvconf/resolv.conf.d/head

	sed -i s/^nameserver/\#\ nameserver/g ${conf}

	if [ ! -n ${DNS} ]; then
		echo "Undefined DNS!"
		return 1
	fi

	local data=
	local item=
	for item in ${DNS[@]}; do
		if [ -n ${item} ]; then
			local match=$(grep "# nameserver ${item}" ${conf})
			if [ -n "${match}" ]; then
				sed -i s/^\#\ nameserver\ ${item}/nameserver\ ${item}/g ${conf}
			else
				echo "nameserver ${item}" >> ${conf}
			fi
		fi
	done

	if [ -f ${conf} ]; then
		resolvconf -u
		echo "update /etc/resolv.conf"
		cat /etc/resolv.conf
	fi
}

is_match() {
	local rs=0
	[ -n "${1}" ] && [ -n "${2}" ] && [ `echo "${1}" | grep -P "${2}"` ] && rs=1
	echo ${rs}
}

is_int() {
	local rs=$(is_match "${1}" "^[0-9]+$")
	if [[ "${rs}" -gt 0 && $(is_match "${2}" "^[0-9]+$") -gt 0 && $(is_match "${3}" "^[0-9]+$") -gt 0 ]]; then
		if [ "${3}" -gt "${2}" ]; then
			if [[ "${1}" -gt "${2}" && "${1}" -lt "${3}" ]]; then
				rs=1
			else
				rs=0
			fi
#		elif [ "${2}" -gt "${3}" ]; then
#			if [[ "${1}" -gt "${3}" && "${1}" -lt "${2}" ]]; then
#				rs=1
#			else
#				rs=0
#			fi
		else
			if [[ "${1}" -gt "${3}" && "${1}" -lt "${2}" ]]; then
				rs=1
			else
				rs=0
			fi
		fi
	fi
	echo ${rs}
}

str_push() {
	local spr=" "
	[ -n "${1}" ] && spr="${1}"

	local words="${2}"
	if [ -n "${3}" ]; then
		[ -n "${words}" ] && words+="${spr}"
		words+="${3}"
	fi
	echo ${words}
}